import 'dart:async';
import 'dart:io';

import 'package:dio/dio.dart';

import '../config/service_url.dart';

//Dart中可选参数，在参数外面加个{}
Future request(url, {formData}) async {
  try {
    print("开始获取数据………………");
    Response response;
    Dio dio = new Dio();
    //抓包使用
    /* (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
        (client) {
      client.findProxy = (uri) {
        return "PROXY 192.168.0.101:8888";
      };
    };*/
/*    client.badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;*/ //加上这个才能抓Https
    dio.options.contentType =
        ContentType.parse("application/x-www-form-urlencoded");
    if (formData == null) {
      response = await dio.post(servicePath[url]);
    } else {
      response = await dio.post(servicePath[url], data: formData);
    }
    if (response.statusCode == 200) {
      return response.data;
    } else {
      throw Exception("后端接口出现异常");
    }
  } catch (e) {
    return print("ERROR：=========>${e}");
  }
}

//获取首页主题内容
Future getHomePageContent() async {
  try {
    print("开始获取首页数据………………");
    Response response;
    Dio dio = new Dio();
    dio.options.contentType =
        ContentType.parse("application/x-www-form-urlencoded");
    var formData = {"lon": "115.02932", "lat": "35.76189"};       
    response = await dio.post(servicePath["homePageContent"], data: formData);
    if (response.statusCode == 200) {
      return response.data;
    } else {
      throw Exception("后端接口出现异常");
    }
  } catch (e) {
    return print("ERROR：=========>${e}");
  }
}

//获取火爆专区
Future getHomePageBelowContent() async {
  try {
    print("开始获取火爆专区数据………………");
    Response response;
    Dio dio = new Dio();
    dio.options.contentType =
        ContentType.parse("application/x-www-form-urlencoded");
    int page = 1;
    response = await dio.post(servicePath["homePageBelowConten"], data: page);
    if (response.statusCode == 200) {
      return response.data;
    } else {
      throw Exception("后端接口出现异常");
    }
  } catch (e) {
    return print("ERROR：=========>${e}");
  }
}
